package com.qdxwx.data.service.picture;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.picture.PictureMapper;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public int insert(Picture picture) {
        picture.setUpdateTime(LocalDateTime.now());
        picture.setCreateTime(LocalDateTime.now());
        return pictureMapper.insertPicture(picture);
    }

    @Override
    public Picture get(int id) {
        if (id < 1) return null;
        return pictureMapper.selectById(id);
    }

    @Override
    public List<Picture> getList(PictureQuery picture, PageRequest page) {
        if (picture.getAlbumId() != null && picture.getAlbumId() > 0) {
            return pictureMapper.selectListByAlbum(picture, page);
        }
        return pictureMapper.selectList(picture, page);
    }

    @Override
    public int count(PictureQuery picture) {
        if (picture.getAlbumId() != null && picture.getAlbumId() > 0) {
            return pictureMapper.countByAlbum(picture);
        }
        return pictureMapper.count(picture);
    }

    @Override
    public int update(Picture picture) {
        if (picture.getId() < 1) return 0;
        picture.setUpdateTime(LocalDateTime.now());
        return pictureMapper.updatePicture(picture);
    }

    @Override
    public int deleteById(int id) {
        if (id < 1) return 0;
        return pictureMapper.deleteById(id);
    }

    @Override
    public int updateView(int id) {
        if (id < 1) return 0;
        return pictureMapper.updateView(id);
    }

    @Override
    public int updateDown(int id) {
        if (id < 1) return 0;
        return pictureMapper.updateDown(id);
    }

    @Override
    public List<Picture> getListByAlbumId(int albumId, PageRequest page) {
        if (albumId < 1) return new ArrayList<>();
        PictureQuery query = new PictureQuery();
        query.setAlbumId(albumId);
        return pictureMapper.selectListByAlbum(query, page);
    }

    @Override
    public int countByAlbumId(int albumId) {
        if (albumId < 1) return 0;
        PictureQuery query = new PictureQuery();
        query.setAlbumId(albumId);
        return pictureMapper.countByAlbum(query);
    }
}
